<?php
namespace BoxSns\Logic;
class LogLogic extends \LogicController {

	public function _initialize() {
		parent::_initialize();
		$this->molde = BoxModel("SystemLog");
	}

	/************用户system_member_statistics流水************/
	public function add($row, $debug = 1) {
		//dict("log_type",array('money'=>1,'xuebazi'=>2));
		//$log_type=dict("log_type");
		//$user_id=$row['user_id'];
		$row['from'] = S('UserAgent_' . $this->user_id) ? S('UserAgent_' . $this->user_id) : intval(S('UserAgent_' . $row['user_id']));
		$row['from'] = intval($row['from'] ? $row['from'] : 1);
		$num = $row['num'];
		$member_model = BoxModel("SystemMember");
		// $store_model = BoxModel("SystemStoreStatistics");
		$row['add_date'] = date("Y-m-d H:i:s");
		$row['public'] = 1;
		//使用模板

		if ($row['event']) {
			$template = BoxModel("system_log_template")->where(array('event' => $row['event']))->find();
			$row['num'] = $row['num'] ? $row['num'] : $template['num'];
			$this->data = $row;
			$row['type'] = $template['type'];
			//dump($template['content']);
			if ($template['content']) {
				$row['remark'] = $this->fetch("", stripslashes(htmlspecialchars_decode($template['content'])));
			}
			$row['public'] = $template['public']; //是否公开显示
		}

		$type = $row['type'];
		$event = $row['event'];
		$params = $row;
		hook("log_before_add", $params);
		hook("log_before_add_" . $type, $params);
		hook("log_before_add_" . $event, $params);
		$row = $this->molde->create($row);
		//dump($row);exit();
		if ($debug == 2) {
			dump($row);
		}
		if ($row['num'] == 0) {
//没有数值返回
			return;
		}
		$logId = $this->molde->add($row);
		if ($num > 0) {
			$field_type = "_inc"; //增加记录
		}
		if ($num < 0) {
			$field_type = "_dec"; //减少记录
		}
		/*******用户的资金***/
		$field = $row['type'];
		$user_id = $row['user_id'];
		//$store_id = $row['store_id'];
		if ($logId && $params['logType'] != 2) {
			$r2 = $member_model->update_statistics($user_id, $field, $num);
			$r4 = $member_model->update_statistics($user_id, $field . $field_type, $num);
		}
		/*******商家的资金***/
		// if ($row['store_id']) {
		//            $r3 = $store_model->update_statistics($store_id, $field, $num);
		//            $r5 = $store_model->update_statistics($user_id, $field . $field_type, $num);
		//        }
		if ($logId) {
			//	dump($row);
			hook("log_after_add", $params);
			hook("log_after_add_" . $type, $params);
			hook("log_after_add_" . $event, $params);
			return array(
				'error' => 0,
				'logId' => $logId,
				'update' => $r2,
				'msg' => '保存成功',
			);
		} else {
			return array(
				'error' => 2,
				'update' => $r2,
				'msg' => '数据保存失败',
			);
		}
	}

}
